package com.morehair.config;

import com.morehair.bean.pojo.MallSystem;
import com.morehair.mapper.MallSystemMapper;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

@Data
@Configuration
public class MallConfig {

    @Autowired
    MallSystemMapper mallSystemMapper;

    Boolean wxShare;

    Integer wxIndexTopic;    //专题精选栏目显示数量
    Integer wxIndexNew;      //新品首发栏目显示数量
    Integer wxIndexHot;      //人气推荐栏目显示数量
    Integer wxIndexBrand;    //品牌直供栏目显示数量
    Integer catlogList;      //分类栏目显示数量
    Integer catlogGoods;     //分类栏目商品显示数量

    Integer orderUnpaid;     //用户下单后超时
    Integer orderUnConfirm;  //订单发货后超期
    Integer orderComment;    //确认收货后超期

    Double expressFreightValue;     //运费金额
    Double expressFreightMin;       //免运费要求消费

    String mallQQ;
    String mallPhone;
    String mallName;
    String mallAddress;

    public MallConfig(MallSystemMapper mallSystemMapper) {
        //refreshMallConfig();
        List<MallSystem> configs = mallSystemMapper.selectByExample(null);
        for (MallSystem config : configs) {
            String key = config.getKeyName();
            String value = config.getKeyValue();
            key = key.substring(key.lastIndexOf("_") + 1);


            Class<MallConfig> mallConfigClass = MallConfig.class;
            Field[] fields = mallConfigClass.getDeclaredFields();
            for (Field field : fields) {
                if (field.getName().toLowerCase().contains(key)) {
                    Class<?> fieldType = field.getType();
                    String setterName = "set" + StringUtils.capitalize(field.getName());
                    Method setterMethod = null;
                    try {
                        setterMethod = mallConfigClass.getDeclaredMethod(setterName, fieldType);
                        if (fieldType.equals(Integer.class)) {
                            setterMethod.invoke(this, Integer.parseInt(value));
                        } else if (fieldType.equals(Double.class)) {
                            setterMethod.invoke(this, Double.parseDouble(value));
                        } else if (fieldType.equals(String.class)) {
                            setterMethod.invoke(this, value);
                        }
                    } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    public void refreshMallConfig() {
        List<MallSystem> configs = mallSystemMapper.selectByExample(null);
        for (MallSystem config : configs) {
            String key = config.getKeyName();
            String value = config.getKeyValue();
            key = key.substring(key.lastIndexOf("_") + 1);


            Class<MallConfig> mallConfigClass = MallConfig.class;
            Field[] fields = mallConfigClass.getDeclaredFields();
            for (Field field : fields) {
                if (field.getName().toLowerCase().contains(key)) {
                    Class<?> fieldType = field.getType();
                    String setterName = "set" + StringUtils.capitalize(field.getName());
                    Method setterMethod = null;
                    try {
                        setterMethod = mallConfigClass.getDeclaredMethod(setterName, fieldType);
                        if (fieldType.equals(Integer.class)) {
                            setterMethod.invoke(this, Integer.parseInt(value));
                        } else if (fieldType.equals(Double.class)) {
                            setterMethod.invoke(this, Double.parseDouble(value));
                        } else if (fieldType.equals(String.class)) {
                            setterMethod.invoke(this, value);
                        }
                    } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
